<?php


namespace app\index\controller;


use app\BaseController;
use app\Request;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    // 首页
    public function index(){
        /*$id = input('id');
        $data = Db::table('auth_rule')->where('id',$id)->find();
        $add = [
            'pid' => $id,
            'name' => $data['name'].'/add',
            'title' => '添加',
            'icon' => 'fa fa-circle-o',
            'status' => 'normal'
        ];
        $del = [
            'pid' => $id,
            'title' => '删除',
            'name' => $data['name'].'/del',
            'icon' => 'fa fa-circle-o',
            'status' => 'normal'
        ];
        $edit = [
            'pid' => $id,
            'name' => $data['name'].'/edit',
            'title' => '修改',
            'icon' => 'fa fa-circle-o',
            'status' => 'normal'
        ];
        $index = [
            'pid' => $id,
            'name' => $data['name'].'/index',
            'title' => '查看',
            'icon' => 'fa fa-circle-o',
            'status' => 'normal'
        ];
        Db::table('auth_rule')->insert($add);
        Db::table('auth_rule')->insert($del);
        Db::table('auth_rule')->insert($edit);
        Db::table('auth_rule')->insert($index);
        die;*/
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'index_title' => $this->_index_title(),
            '_about_index' =>$this->_about_index(),
            '_about_list' =>$this->_about_list(),
            '_case_list' => $this->_case_list(),
            '_pro_list' => $this->_pro_list(),
            '_blog_list' => $this->_blog_list()
        ]);
//        dump($this->_about_index()['images']);die;
        return view("index");
    }
    public function _index_title()
    {
        return Db::table('index_title')->select();
    }
    public function _about_index(){
        $data = Db::table('about')->where('state',1)->order('id desc')->find();
        $images = explode(',',$data['images']);
        return ['data' => $data,'images' => $images];
    }
    public function _about_list()
    {
        return Db::table('about')->where('state',1)->order("id desc")->limit(5)->select();
    }
    //首页案例
    public function _case_list()
    {
        $data = Db::table('case')->where('state',1)->select();
        return $data;
    }
    //产品展示
    public function _pro_list()
    {
        $data = Db::table('portfolio')->where('state',1)->select();
        return $data;
    }
    //新闻咨询
    public function _blog_list()
    {
        $data = Db::table('blog')->limit(3)->where('state',1)->order('id desc')->select();
        return $data;
    }

    //关于我们
    public function about(){
        $id = input('id');
        if(empty($id) || !isset($id)){
            $about = Db::table('about')->order('id desc')->find();
        }else{
            $about = Db::table('about')->where('id',$id)->find();
        }
        $image_arr = explode(',',$about['images']);
        $about_list = Db::table('about')->limit(5)->select();
        $live = Db::table('live')->select();
        $about_set = Db::table('about_set')->select();
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'about' => $about,
            'about_list' => $about_list,
            'image_arr' => $image_arr,
            'live' => $live,
            'about_set' => $about_set
        ]);
        return view("about");
    }
    public function about_detail()
    {
        $id = input('id');
        $data = Db::table('about')->where('id',$id)->find();
        $list = Db::table('about')->select();
        $this->assign([
            'data' => $data,
            'list' => $list
        ]);
        return view("detail");
    }
    //案例欣赏
    public function case(){
        $case_list = Db::table('case')->limit(7)->select();
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'case_list' => $case_list
        ]);
        return view("case");
    }
    //案例欣赏详情
    public function caseDetail()
    {
        $case_id = input('case_id');
        $data = Db::table('case')->where('id',$case_id)->find();
        $list = Db::table('case')->select();
        View::assign([
            'data' => $data,
            'list' => $list,
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting()
        ]);
        return view('case_detail');
    }

    //产品展示
    public function portfolio(){
        $portfolio_list = Db::table('portfolio')->select();
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'portfolio_list' => $portfolio_list
        ]);
        return view("portfolio");
    }

//    新闻咨询
    public function blog(){
        $blog_list = Db::table('blog')->paginate(6);
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'blog_list' => $blog_list
        ]);
        return view("blog");
    }
    public function blog_detail($id){
        $data = Db::table('blog')->find($id);
        $list = Db::table('blog')->select();
        View::assign([
            'navigation' => getNavigationAll(),
            'nav' => getNavigation(),
            'setting' => setting(),
            'data' => $data,
            'list' => $list
        ]);
        return view('blog_detail');
    }

    //联系我们
    public function contact(Request $request){
        if($request->post()){
            $name = input('name');
            $email = input('email');
            $mobile = input('mobile');
            $content = input('content');
            if(empty($name)) $this->error('姓名不能为空','index/index/contact');
            if(empty($mobile)) $this->error('电话不能为空','index/index/contact');
            if(empty($content)) $this->error('内容不能为空','index/index/contact');
            $data = [
                'name' => $name,
                'email' => $email,
                'mobile' => $mobile,
                'content' => $content,
                'ip' => $request->ip(),
                'ctime' => time()
            ];
            $row = Db::table('contact')->insert($data);
            if(!$row) $this->error('网路错误 请刷新后重试','index/index/contact');
            $this->success('已提交成功 请等待工作人员联系','index/index/index');
        }
        View::assign([
            'navigation' => getNavigationAll(),
            'setting' => setting(),
            'nav' => getNavigation()
        ]);
        return view("contact");
    }
    public function insert_contact(Request $request)
    {
        $name = input('name');
        $email = input('email');
        $mobile = input('mobile');
        $content = input('content');
        if(empty($name)) $this->error('姓名不能为空','index/index/contact');
        if(empty($mobile)) $this->error('电话不能为空','index/index/contact');
        if(empty($content)) $this->error('内容不能为空','index/index/contact');
        $data = [
            'name' => $name,
            'email' => $email,
            'mobile' => $mobile,
            'content' => $content,
            'ip' => $request->ip(),
            'ctime' => time()
        ];
        $row = Db::table('contact')->insert($data);
        if(!$row) $this->error('网路错误 请刷新后重试','index/index/contact');
        $this->success('已提交成功 请等待工作人员联系','index/index/index');
    }
}